package tw.miku.protocol.cus;

import java.nio.charset.CharacterCodingException;

import org.apache.mina.core.session.IoSession;

import tw.miku.GameMain;
import tw.miku.model.Group;
import tw.miku.model.Room;
import tw.miku.model.User;
import tw.miku.protocol.codec.AbstractRequestMessagePack;
import tw.miku.protocol.codec.room.ExitRoomRequestPack;
import tw.miku.protocol.codec.user.GetReadyRequestPack;
import tw.miku.protocol.codec.user.GetReadyResponsePack;

public class GetReadyHandler implements IMessageHandler
{

	public GetReadyHandler()
	{
	}

	@Override
	public void handleData(AbstractRequestMessagePack message, IoSession session)
			throws CharacterCodingException
	{
		GetReadyRequestPack pack = (GetReadyRequestPack) message;
		if (null != pack)
		{
			Group group = GameMain.getGroup();
			User user = group.getUser(session.getId());
			Room room = user.getRoom();
			if (null != user && null != room)
			{
				room.setUserReady(user.getId(), pack.isReady);
			}
			else
			{
				GetReadyResponsePack result = new GetReadyResponsePack();
				result.result = 1;
				session.write(result.encode());
			}
		}

	}

}
